﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace _10_多线程框架
{
    public partial class Form1 : Form
    {
        //随机延时
        Random rd = new Random();

        //线程-1
        Thread thread1 = null;
        ManualResetEvent thread1ManualResetEvent =null;

        //线程-2
        Thread thread2 = null;
        ManualResetEvent thread2ManualResetEvent = null;

        //线程-3
        Thread thread3 = null;
        ManualResetEvent thread3ManualResetEvent = null;

        //线程-4
        Thread thread4 = null;
        ManualResetEvent thread4ManualResetEvent = null;

        //线程-5
        Thread thread5 = null;
        ManualResetEvent thread5ManualResetEvent = null;



        public Form1()
        {
            InitializeComponent();
            //取消跨线程访问控件检查
            Control.CheckForIllegalCrossThreadCalls = false;
            //设置指定控件的布局方式,设置控件跨4行
            tableLayoutPanel4.SetRowSpan(this.listBox1, 4);
        }

        #region 线程-1

        /// <summary>
        /// 启动
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnStart1_Click(object sender, EventArgs e)
        {
            if (this.btnStart1.Text =="启 动")
            {
                //创建线程
                thread1 = new Thread(thread1DoWork);
                thread1.IsBackground = true;
                thread1.Start();
                //创建手动同步事件
                thread1ManualResetEvent = new ManualResetEvent(true);
                //设置按钮文本
                this.btnStart1.Text = "停 止";
                //挂起可以点击
                this.btnSuspend1.Enabled = true;
            }
            else
            {
                thread1.Abort();
                this.btnStart1.Text = "启 动";
                //挂起不可以点击
                this.btnSuspend1.Enabled = false;
            }

        }
        /// <summary>
        /// 线程-1的方法
        /// </summary>
        public void thread1DoWork()
        {
            for (int i = 0; i <= 100; i++)
            {
                //判断是否挂起
                thread1ManualResetEvent.WaitOne();

                //更新进度
                this.progressBar1.Value = i;
                //更新文本
                this.lbl_value1.Text = i + "%";

                int delay = 0;
                //判断是随机延时还是固定延时
                if (radioButton1.Checked == true)
                {
                    delay = int.Parse(textBox1.Text);
                }
                else
                {
                    int min = int.Parse(textBox2.Text);
                    int max = int.Parse(textBox3.Text);
                    delay = rd.Next(min, max);
                }
                Thread.Sleep(delay);
            }
        }
        /// <summary>
        /// 挂 起
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnSuspend1_Click(object sender, EventArgs e)
        {
            if (this.btnSuspend1.Text == "挂 起")
            {
                thread1ManualResetEvent.Reset();
                this.btnStart1.Enabled = false;
                this.btnSuspend1.Text = "唤 醒";
            }
            else
            {
                thread1ManualResetEvent.Set();
                this.btnStart1.Enabled = true;
                this.btnSuspend1.Text = "挂 起";
            }

        }

        #endregion

        #region 线程-2

        /// <summary>
        /// 启动
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnStart2_Click(object sender, EventArgs e)
        {
            if (this.btnStart2.Text == "启 动")
            {
                //创建线程
                thread2 = new Thread(thread2DoWork);
                thread2.IsBackground = true;
                thread2.Start();
                //创建手动同步事件
                thread2ManualResetEvent = new ManualResetEvent(true);
                //设置按钮文本
                this.btnStart2.Text = "停 止";
                //挂起可以点击
                this.btnSuspend2.Enabled = true;
            }
            else
            {
                thread2.Abort();
                this.btnStart2.Text = "启 动";
                //挂起不可以点击
                this.btnSuspend2.Enabled = false;
            }

        }
        /// <summary>
        /// 线程-1的方法
        /// </summary>
        public void thread2DoWork()
        {
            for (int i = 0; i <= 100; i++)
            {
                //判断是否挂起
                thread2ManualResetEvent.WaitOne();

                //更新进度
                this.progressBar2.Value = i;
                //更新文本
                this.lbl_value2.Text = i + "%";

                int delay = 0;
                //判断是随机延时还是固定延时
                if (radioButton1.Checked == true)
                {
                    delay = int.Parse(textBox1.Text);
                }
                else
                {
                    int min = int.Parse(textBox2.Text);
                    int max = int.Parse(textBox3.Text);
                    delay = rd.Next(min, max);
                }
                Thread.Sleep(delay);
            }
        }
        /// <summary>
        /// 挂 起
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnSuspend2_Click(object sender, EventArgs e)
        {
            if (this.btnSuspend2.Text == "挂 起")
            {
                thread2ManualResetEvent.Reset();
                this.btnStart2.Enabled = false;
                this.btnSuspend2.Text = "唤 醒";
            }
            else
            {
                thread2ManualResetEvent.Set();
                this.btnStart2.Enabled = true;
                this.btnSuspend2.Text = "挂 起";
            }

        }

        #endregion

        #region 线程-3

        /// <summary>
        /// 启动
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnStart3_Click(object sender, EventArgs e)
        {
            if (this.btnStart3.Text == "启 动")
            {
                //创建线程
                thread3 = new Thread(thread3DoWork);
                thread3.IsBackground = true;
                thread3.Start();
                //创建手动同步事件
                thread3ManualResetEvent = new ManualResetEvent(true);
                //设置按钮文本
                this.btnStart3.Text = "停 止";
                //挂起可以点击
                this.btnSuspend3.Enabled = true;
            }
            else
            {
                thread3.Abort();
                this.btnStart3.Text = "启 动";
                //挂起不可以点击
                this.btnSuspend3.Enabled = false;
            }

        }
        /// <summary>
        /// 线程-1的方法
        /// </summary>
        public void thread3DoWork()
        {
            for (int i = 0; i <= 100; i++)
            {
                //判断是否挂起
                thread3ManualResetEvent.WaitOne();

                //更新进度
                this.progressBar3.Value = i;
                //更新文本
                this.lbl_value3.Text = i + "%";

                int delay = 0;
                //判断是随机延时还是固定延时
                if (radioButton1.Checked == true)
                {
                    delay = int.Parse(textBox1.Text);
                }
                else
                {
                    int min = int.Parse(textBox2.Text);
                    int max = int.Parse(textBox3.Text);
                    delay = rd.Next(min, max);
                }
                Thread.Sleep(delay);
            }
        }
        /// <summary>
        /// 挂 起
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnSuspend3_Click(object sender, EventArgs e)
        {
            if (this.btnSuspend3.Text == "挂 起")
            {
                thread3ManualResetEvent.Reset();
                this.btnStart3.Enabled = false;
                this.btnSuspend3.Text = "唤 醒";
            }
            else
            {
                thread3ManualResetEvent.Set();
                this.btnStart3.Enabled = true;
                this.btnSuspend3.Text = "挂 起";
            }

        }

        #endregion

        #region 线程-4

        /// <summary>
        /// 启动
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnStart4_Click(object sender, EventArgs e)
        {
            if (this.btnStart4.Text == "启 动")
            {
                //创建线程
                thread4 = new Thread(thread4DoWork);
                thread4.IsBackground = true;
                thread4.Start();
                //创建手动同步事件
                thread4ManualResetEvent = new ManualResetEvent(true);
                //设置按钮文本
                this.btnStart4.Text = "停 止";
                //挂起可以点击
                this.btnSuspend4.Enabled = true;
            }
            else
            {
                thread4.Abort();
                this.btnStart4.Text = "启 动";
                //挂起不可以点击
                this.btnSuspend4.Enabled = false;
            }

        }
        /// <summary>
        /// 线程-1的方法
        /// </summary>
        public void thread4DoWork()
        {
            for (int i = 0; i <= 100; i++)
            {
                //判断是否挂起
                thread4ManualResetEvent.WaitOne();

                //更新进度
                this.progressBar4.Value = i;
                //更新文本
                this.lbl_value4.Text = i + "%";

                int delay = 0;
                //判断是随机延时还是固定延时
                if (radioButton1.Checked == true)
                {
                    delay = int.Parse(textBox1.Text);
                }
                else
                {
                    int min = int.Parse(textBox2.Text);
                    int max = int.Parse(textBox3.Text);
                    delay = rd.Next(min, max);
                }
                Thread.Sleep(delay);
            }
        }
        /// <summary>
        /// 挂 起
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnSuspend4_Click(object sender, EventArgs e)
        {
            if (this.btnSuspend4.Text == "挂 起")
            {
                thread4ManualResetEvent.Reset();
                this.btnStart4.Enabled = false;
                this.btnSuspend4.Text = "唤 醒";
            }
            else
            {
                thread4ManualResetEvent.Set();
                this.btnStart4.Enabled = true;
                this.btnSuspend4.Text = "挂 起";
            }

        }

        #endregion

        #region 线程-5

        /// <summary>
        /// 启动
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnStart5_Click(object sender, EventArgs e)
        {
            if (this.btnStart5.Text == "启 动")
            {
                //创建线程
                thread5 = new Thread(thread5DoWork);
                thread5.IsBackground = true;
                thread5.Start();
                //创建手动同步事件
                thread5ManualResetEvent = new ManualResetEvent(true);
                //设置按钮文本
                this.btnStart5.Text = "停 止";
                //挂起可以点击
                this.btnSuspend5.Enabled = true;
            }
            else
            {
                thread5.Abort();
                this.btnStart5.Text = "启 动";
                //挂起不可以点击
                this.btnSuspend5.Enabled = false;
            }

        }
        /// <summary>
        /// 线程-1的方法
        /// </summary>
        public void thread5DoWork()
        {
            for (int i = 0; i <= 100; i++)
            {
                //判断是否挂起
                thread5ManualResetEvent.WaitOne();

                //更新进度
                this.progressBar5.Value = i;
                //更新文本
                this.lbl_value5.Text = i + "%";

                int delay = 0;
                //判断是随机延时还是固定延时
                if (radioButton1.Checked == true)
                {
                    delay = int.Parse(textBox1.Text);
                }
                else
                {
                    int min = int.Parse(textBox2.Text);
                    int max = int.Parse(textBox3.Text);
                    delay = rd.Next(min, max);
                }
                Thread.Sleep(delay);
            }
        }
        /// <summary>
        /// 挂 起
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnSuspend5_Click(object sender, EventArgs e)
        {
            if (this.btnSuspend5.Text == "挂 起")
            {
                thread5ManualResetEvent.Reset();
                this.btnStart5.Enabled = false;
                this.btnSuspend5.Text = "唤 醒";
            }
            else
            {
                thread5ManualResetEvent.Set();
                this.btnStart5.Enabled = true;
                this.btnSuspend5.Text = "挂 起";
            }

        }


        #endregion



        /// <summary>
        /// 批量启动
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void button11_Click(object sender, EventArgs e)
        {
            btnStart1_Click(null, null);
            btnStart2_Click(null, null);
            btnStart3_Click(null, null);
            btnStart4_Click(null, null);
            btnStart5_Click(null, null);

            this.button11.Enabled = false;
            this.button12.Enabled = true;
            this.button14.Enabled = true;
        }
        /// <summary>
        /// 批量挂起
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void button12_Click(object sender, EventArgs e)
        {
            btnSuspend1_Click(null, null);
            btnSuspend2_Click(null, null);
            btnSuspend3_Click(null, null);
            btnSuspend4_Click(null, null);
            btnSuspend5_Click(null, null);

            this.button12.Enabled = false;
            this.button13.Enabled = true;
            this.button14.Enabled = false;
        }
        /// <summary>
        /// 批量唤醒
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void button13_Click(object sender, EventArgs e)
        {
            btnSuspend1_Click(null, null);
            btnSuspend2_Click(null, null);
            btnSuspend3_Click(null, null);
            btnSuspend4_Click(null, null);
            btnSuspend5_Click(null, null);

            this.button12.Enabled = true;
            this.button13.Enabled = false;
            this.button14.Enabled = true;
        }
        /// <summary>
        /// 批量停止
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void button14_Click(object sender, EventArgs e)
        {
            btnStart1_Click(null, null);
            btnStart2_Click(null, null);
            btnStart3_Click(null, null);
            btnStart4_Click(null, null);
            btnStart5_Click(null, null);

            this.button11.Enabled = true;
            this.button12.Enabled = false;
            this.button13.Enabled = false;
            this.button14.Enabled = false;
            this.button15.Enabled = true;

        }

        /// <summary>
        /// 清空进度条
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void button15_Click(object sender, EventArgs e)
        {
            this.progressBar1.Value = 0;
            this.progressBar2.Value = 0;
            this.progressBar3.Value = 0;
            this.progressBar4.Value = 0;
            this.progressBar5.Value = 0;

            this.lbl_value1.Text = "0%";
            this.lbl_value2.Text = "0%";
            this.lbl_value3.Text = "0%";
            this.lbl_value4.Text = "0%";
            this.lbl_value5.Text = "0%";
        }
    }
}
